/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2019年6月23日
 * <修改描述:>
 */
package com.tx.component.task.starter;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import com.tx.component.task.TaskContextConstants;
import com.tx.component.task.dao.TaskDefDao;
import com.tx.component.task.dao.TaskDetailDao;
import com.tx.component.task.dao.TaskExecuteLogDao;
import com.tx.component.task.dao.TaskStatusDao;
import com.tx.component.task.dao.impl.TaskDefDaoMybatisImpl;
import com.tx.component.task.dao.impl.TaskDetailDaoMybatisImpl;
import com.tx.component.task.dao.impl.TaskExecuteLogDaoMybatisImpl;
import com.tx.component.task.dao.impl.TaskStatusDaoMybatisImpl;
import com.tx.component.task.service.TaskContextRegistry;
import com.tx.component.task.service.TaskDefService;
import com.tx.component.task.service.TaskDetailService;
import com.tx.component.task.service.TaskExecuteLogService;
import com.tx.component.task.service.TaskStatusService;
import com.tx.component.task.service.impl.TaskContextRegistryImpl;
import com.tx.component.task.service.impl.TaskDefServiceImpl;
import com.tx.component.task.service.impl.TaskDetailServiceImpl;
import com.tx.component.task.service.impl.TaskExecuteLogServiceImpl;
import com.tx.component.task.service.impl.TaskStatusServiceImpl;
import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.mybatis.support.MyBatisDaoSupport;
import com.tx.core.starter.component.ComponentConstants;

/**
 * 权限容器自动配置项<br/>
 * <功能详细描述>
 * 
 * @author  Administrator
 * @version  [版本号, 2019年6月23日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@Configuration
public class ImportTaskContextConfiguration {
    
    /**
     * mybatis持久层逻辑实现<br/>
     * <功能详细描述>
     * 
     * @author  Administrator
     * @version  [版本号, 2019年5月2日]
     * @see  [相关类/方法]
     * @since  [产品/模块版本]
     */
    @Configuration
    @ConditionalOnProperty(prefix = ComponentConstants.PERSISTER_PROPERTIES_PREFIX, value = "type", havingValue = "mybatis")
    @ConditionalOnBean(name = { "tx.component.myBatisDaoSupport" })
    public static class MybatisAuthContextPersisterConfiguration
            implements InitializingBean {
        
        /** 容器所属模块：当该值为空时，使用spring.application.name的内容 */
        private String module;
        
        /** 任务容器属性 */
        private TaskContextProperties properties;
        
        /** application.name */
        @Value(value = "${spring.application.name}")
        private String applicationName;
        
        /** mybatis属性 */
        @Resource(name = "tx.component.myBatisDaoSupport")
        private MyBatisDaoSupport myBatisDaoSupport;
        
        /** 事务管理器 */
        @SuppressWarnings("unused")
        private TransactionTemplate transactionTemplate;
        
        /** <默认构造函数> */
        public MybatisAuthContextPersisterConfiguration(
                TaskContextProperties properties,
                PlatformTransactionManager transactionManager) {
            this.properties = properties;
            this.transactionTemplate = new TransactionTemplate(
                    transactionManager);
        }
        
        /**
         * @throws Exception
         */
        @Override
        public void afterPropertiesSet() throws Exception {
            AssertUtils.notNull(this.myBatisDaoSupport,
                    "myBatisDaoSupport is null.");
            
            //初始化包名
            if (!StringUtils.isBlank(this.applicationName)) {
                this.module = this.applicationName;
            }
            if (!StringUtils.isEmpty(this.properties.getModule())) {
                this.module = this.properties.getModule();
            }
            AssertUtils.notEmpty(this.module, "module is empty.");
        }
        
        /**
         * 任务定义持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(name = TaskContextConstants.BEAN_NAME_TASK_DEF_DAO)
        public TaskDefDao taskDefDao() {
            TaskDefDaoMybatisImpl dao = new TaskDefDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 任务状态持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthItemDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(name = TaskContextConstants.BEAN_NAME_TASK_STATUS_DAO)
        public TaskStatusDao taskStatusDao() {
            TaskStatusDaoMybatisImpl dao = new TaskStatusDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 任务执行日志持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthRefItemDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_EXECUTE_LOG_DAO)
        public TaskExecuteLogDao taskExecuteLogDao() {
            TaskExecuteLogDaoMybatisImpl dao = new TaskExecuteLogDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 任务详情持久层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return TaskDetailDao [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_DETAIL_DAO)
        public TaskDetailDao taskDetailDao() {
            TaskDetailDaoMybatisImpl dao = new TaskDetailDaoMybatisImpl();
            dao.setMyBatisDaoSupport(this.myBatisDaoSupport);
            return dao;
        }
        
        /**
         * 任务定义业务层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthTypeItemService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_DEF_SERVICE)
        public TaskDefService taskDefService(TaskDefDao taskDefDao) {
            TaskDefService service = new TaskDefServiceImpl(this.module,
                    taskDefDao);
            return service;
        }
        
        /**
         * 任务状态业务层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthItemService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_STATUS_SERVICE)
        public TaskStatusService taskStatusService(
                TaskStatusDao taskStatusDao) {
            TaskStatusService service = new TaskStatusServiceImpl(
                    taskStatusDao);
            return service;
        }
        
        /**
         * 任务执行日志业务层<br/>
         * <功能详细描述>
         * @return [参数说明]
         * 
         * @return AuthRefItemService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_EXECUTE_LOG_SERVICE)
        public TaskExecuteLogService taskExecuteLogService(
                TaskExecuteLogDao taskExecuteLogDao) {
            TaskExecuteLogService service = new TaskExecuteLogServiceImpl(
                    this.module, taskExecuteLogDao);
            return service;
        }
        
        /**
         * 任务容器注册机<br/>
         * <功能详细描述>
         * @param taskDefService
         * @param taskStatusService
         * @return [参数说明]
         * 
         * @return TaskContextRegistry [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_CONTEXT_REGISTRY)
        public TaskContextRegistry taskContextRegistry(
                TaskDefService taskDefService,
                TaskStatusService taskStatusService) {
            TaskContextRegistryImpl registry = new TaskContextRegistryImpl();
            registry.setTaskDefService(taskDefService);
            registry.setTaskStatusService(taskStatusService);
            return registry;
        }
        
        /**
         * 任务详情业务层<br/>
         * <功能详细描述>
         * @param taskDetailDao
         * @return [参数说明]
         * 
         * @return TaskDetailService [返回类型说明]
         * @exception throws [异常类型] [异常说明]
         * @see [类、类#方法、类#成员]
         */
        @Bean(TaskContextConstants.BEAN_NAME_TASK_DETAIL_SERVICE)
        public TaskDetailService taskDetailService(TaskDetailDao taskDetailDao,
                TaskContextRegistry taskContextRegistry) {
            TaskDetailService service = new TaskDetailServiceImpl(taskDetailDao,
                    taskContextRegistry);
            return service;
        }
        
    }
    
}
